#include <algorithm>
#include <cassert>
#include <vector>

using namespace std;

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();

        vector<int> res(n);

        res[0] = 1;
        for (int i = 1; i < n; ++i) {
            res[i] = nums[i - 1] * res[i - 1];
        }

        int right = 1;
        for (int i = n - 1; i >= 0; --i) {
            res[i] = res[i] * right;
            right *= nums[i];
        }

        return res;
    }
};

int main() {
    Solution s;

    vector<int> nums{1, 2, 3, 4};
    vector<int> output{24, 12, 8, 6};

    assert(s.productExceptSelf(nums) == output);

    return 0;
}